<template>
  <div v-loading="loading" class="detail-wrapper">
    <el-empty description="文章不存在" v-if="!data && !loading">
      <el-button type="primary" size="small" @click="goBack">返回上级</el-button>
    </el-empty>
    <div v-if="data">
      <div class="title">{{ data?.title }}</div>
      <el-row :gutter="10" class="category-tag-wrapper">
        <el-col :sm="12" :xs="24" v-if="data?.category">
          <div class="wrapper">
            <span class="category-tag-title">文章类别: </span>
            <RenderDict tag :value="data?.category" />
          </div>
        </el-col>
        <el-col :sm="12" :xs="24" v-if="data?.tag">
          <div class="wrapper">
            <span class="category-tag-title">文章标签: </span>
            <RenderDict tag :value="data?.tag" />
          </div>
        </el-col>
      </el-row>
      <div class="time">{{ data?.createdAt }}</div>
      <div class="desc">{{ data?.desc }}</div>
      <MdPreview :editorId="id" :modelValue="data?.markdown" />
      <!-- <MdCatalog :editorId="id" :scrollElement="scrollElement" /> -->
    </div>
  </div>
</template>
<script setup lang="js">
import { ref, onMounted } from "vue"
import { articleDetailNoTokenApi } from "@/api/article"
import { useRoute, useRouter } from "vue-router"
import { MdPreview, MdCatalog } from 'md-editor-v3';
// preview.css相比style.css少了编辑器那部分样式
import 'md-editor-v3/lib/preview.css';

const id = 'preview-only';
const scrollElement = document.documentElement;
const route = useRoute()
const router = useRouter()

const data = ref();
const loading = ref(false);


const init = async () => {
  loading.value = true;
  // 获取文章详情
  try {
    const id = route.params?.id;
    if (id) {
      const row = await articleDetailNoTokenApi({ id })
      data.value = row;
    }
  } catch (error) {}
  loading.value = false;
}

const goBack = () => {
  router.push('/')
}

onMounted(() => {
  init()
})
</script>
<style scoped lang="less">
.detail-wrapper {
  min-height: 60vh;
}
:deep(.md-editor-preview-wrapper) {
  padding-left: 0;
  padding-right: 0;
}
.time {
  font-weight: 400;
  font-size: 14px;
  line-height: 22px;
  color: #b0b7c2;
  margin-bottom: 8px;
}
.category-tag-wrapper {
  display: flex;
  align-items: center;
  .wrapper {
    margin-right: 48px;
    display: flex;
    align-items: center;
    padding-bottom: 6px;
  }
  .category-tag-title {
    font-weight: 400;
    font-size: 14px;
    line-height: 22px;
    color: #b0b7c2;
    margin-right: 12px;
  }
}
.title {
  font-weight: 400;
  font-size: 32px;
  line-height: 40px;
  color: #1c2026;
  word-break: break-all;
  margin-bottom: 12px;
}
.desc {
  font-weight: 400;
  font-size: 14px;
  line-height: 22px;
  color: #b0b7c2;
}
</style>
